import {login , reqLogout} from "@/api"

export default {
    namespaced: true,
    state:{
        name:localStorage.getItem("name"),
        token:localStorage.getItem("token")
    },
    actions:{
        async toLogin({commit},{phone,password}){
            try {//登陆成功
                const user =await login(phone,password)
                localStorage.setItem("token",user.token)
                localStorage.setItem("name",user.name)
                commit("LOGIN",user)
            } catch (error) {//登录失败
                return error
            }
        },
        //退出登录
        async logout({commit}){
            await reqLogout()
            localStorage.removeItem("token")
            localStorage.removeItem("name")
            commit("LOGOUT")
        }
    },
    mutations:{
        LOGIN(state,user){
            state.name =user.name
            state.token =user.token
        },
        LOGOUT(state){
            state.token=""
            state.name = ''
        }
    }
}